/*
 * @Author: wwssaabb
 * @Date: 2021-06-25 16:02:59
 * @LastEditTime: 2021-06-25 16:39:02
 * @FilePath: \demo\todo_list\src\store\actions.ts
 */

import { State } from "./state";
import { Mutations, MutationType } from "./mutations";
import { ActionContext, ActionTree } from "vuex";

export enum ActionTypes {
  GetTodoList = "GET_TODO_LIST",
}

type ActionArgs = Omit<ActionContext<State, State>, "commit"> & {
  commit<k extends keyof Mutations>(
    key: k,
    payload: Parameters<Mutations[k]>[1]
  ): ReturnType<Mutations[k]>;
};

export type Actions = {
  [ActionTypes.GetTodoList](context: ActionArgs): void;
};

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

export const actions: ActionTree<State, State> & Actions = {
  async [ActionTypes.GetTodoList]({ commit }) {
    commit(MutationType.SetLoading, true);
    await sleep(500);
    commit(MutationType.SetLoading, false);
    commit(MutationType.SetItem, []);
  },
};
